package com.app.qbake.module.publish.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.aliyun.demo.recorder.AliyunVideoRecorder;
import com.wind.base.BaseActivity;
import com.wind.base.utils.ActivityUtil;
import com.wind.base.utils.Navigator;
import com.app.qbake.AppRuntimePermissions;
import com.app.qbake.R;
import com.app.qbake.util.AliPaiUtil;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.util.List;

import static com.wind.base.C.Value.REQUEST_CODE_CAMERA;

public class VideoUploadTipActivity extends BaseActivity {

    public static void start(Context context) {
        Navigator.navigate(context, VideoUploadTipActivity.class);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_upload_tip);

        $(R.id.tv_upload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //权限判断
                AndPermission.with(getActivity())
                        .runtime()
                        .permission(AppRuntimePermissions.CAMERA, AppRuntimePermissions.EXTERNAL_STORAGE)
                        .onGranted(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> permissions) {
                                //MediaUtil.startVideoCapture(getActivity(), REQUEST_CODE_CAMERA);
                                AliPaiUtil.startRecordForResult(getActivity(), REQUEST_CODE_CAMERA);
                            }
                        })
                        .onDenied(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> permissions) {

                            }
                        })
                        .start();


            }
        });
    }

    @Override
    protected void setTitle() {
        mTitleBar.setTitle("上传视频");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        /*if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_CAMERA) {
                Uri uri = data.getData();
                String videoPath=MediaUtil.getVideoPath(getActivity(),uri);
                //启动预览界面
                VideoPreviewActivity.start(getActivity(),videoPath);
                ActivityUtil.finish(getActivity());
            }
        }*/
        if (requestCode == REQUEST_CODE_CAMERA) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                int type = data.getIntExtra(AliyunVideoRecorder.RESULT_TYPE, 0);
                if (type == AliyunVideoRecorder.RESULT_TYPE_RECORD) {

                    String videoPath = data.getStringExtra(AliyunVideoRecorder.OUTPUT_PATH);
                    //ToastUtil.showToast(getActivity(), "视频路径：" + path);
                    VideoPreviewActivity.start(getActivity(),videoPath);
                    ActivityUtil.finish(getActivity());
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                //Toast.makeText(this,"用户取消录制",Toast.LENGTH_SHORT).show();
            }
        }
    }

}
